package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.domain.*;
import cn.edu.gduf.luowowo.article.query.DestinationDaysQuery;
import cn.edu.gduf.luowowo.article.query.DestinationSearchQuery;
import cn.edu.gduf.luowowo.article.query.StrategyCatalogQuery;
import cn.edu.gduf.luowowo.article.query.TravelQuery;
import cn.edu.gduf.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationMonthService destinationMonthService;
    @Reference
    private IDestinationAllthemeService destinationAllthemeService;
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IDestinationDayService destinationDayService;

    @RequestMapping("")
    public String index_destination(Model model,@ModelAttribute("qo") DestinationSearchQuery qo) {
        model.addAttribute("hotRegions", regionService.queryHotRegions());

        //主题推荐
        List<DestinationAlltheme> destinationAlltheme1 = destinationAllthemeService.queryThemeById(qo);
        model.addAttribute("destTagList",destinationAlltheme1);

        List<DestinationAlltheme> destinationAlltheme2 = destinationAllthemeService.queryThemeById2(qo);
        model.addAttribute("destTagList1",destinationAlltheme2);

        List<DestinationAlltheme> destinationAlltheme3 = destinationAllthemeService.queryThemeById3(qo);
        model.addAttribute("destTagList2",destinationAlltheme3);

        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId) {
        List<Destination> list = destinationService.getHotDestByRegionId(regionId);
        if (list != null && list.size() > 0) {
            int middle = list.size() / 2 + 1;
            List<Destination> left = list.subList(0, middle);
            List<Destination> right = list.subList(middle, list.size());
            model.addAttribute("leftDests", left);
            model.addAttribute("rightDests", right);
            model.addAttribute("regionId", regionId);
        }
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public Object guide(Model model, Long id) {
        //toast
        //dest
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //catalogs攻略分类，包括每个攻略下的所有攻略文章
        model.addAttribute("catalogs", strategyCatalogService.queryByDestId(id));
        //查询当前目的地下的攻略文章点击量（阅读量）的前三篇strategyDetails
        model.addAttribute("strategyDetails", strategyDetailService.queryDetailViewNumTop3(id));
        return "destination/guide";
    }


    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //目的地对象
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public Object survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        model.addAttribute("catalogs", strategyCatalogService.queryByDestId(qo.getDestId()));
        model.addAttribute("catalog",strategyCatalogService.get(qo.getCatalogId()));
        List<StrategyDetail> list = strategyDetailService.queryByCatalogId(qo.getCatalogId());
        if (list != null && list.size() > 0) {
            StrategyDetail detail = list.get(0);
            //查询内容
            detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
            model.addAttribute("detail", detail);
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelQuery qo){
        //pageInfo 查询当前目的地下的游记
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }

    //当季推荐
    @RequestMapping("/getDestByMonId")
    public Object getDestByMonId(Model model, Long monthId) {

        //获取当季推荐目的地
        model.addAttribute("destinationAndMon",destinationService.queryDestByMonId(monthId));
        //将得到的月份共享过去
        model.addAttribute("monthId",monthId);
        return "destination/monthTpl";
    }



    //目的地筛选
    @RequestMapping("/destFilter")
    public Object destFilter(Model model, @ModelAttribute("qo") DestinationSearchQuery qo) {

        //大主题
        List<DestinationTheme> destinationThemes = destinationThemeService.listAll();
        model.addAttribute("bigtheme",destinationThemes);
        //月份
        List<DestinationMonth> destinationMonths = destinationMonthService.listAll();
        model.addAttribute("months",destinationMonths);
        //model.addAttribute("monthId",qo.getMonthId());

        //小主题
        model.addAttribute("destTagList",destinationAllthemeService.queryThemeById(qo));  //全年适宜
        model.addAttribute("destTagList1",destinationAllthemeService.queryThemeById2(qo));   //季节
        model.addAttribute("destTagList2",destinationAllthemeService.queryThemeById3(qo));   //出行方式
        //天数
        model.addAttribute("days",destinationDayService.listAll());

        //目的地
        List<Destination> destinations = destinationService.queryDest(qo);
        model.addAttribute("dest",destinations);

        return "destination/destFilter";
    }


    @Reference
    private IScenicPlaceService scenicPlaceService;

    //目的地筛选
    @RequestMapping("/condition")
    public Object condition(Model model, @ModelAttribute("qo") DestinationSearchQuery qo) {

        //目的地
        List<Destination> destinations = destinationService.queryDest(qo);
        model.addAttribute("dest",destinations);


        //景点排名
        //ScenicPlace scenicPlaces = scenicPlaceService.queryDest(id);
        //model.addAttribute("scennn",scenicPlaces);

        return "destination/monthDestThemeTpl";
    }

    //天数范围查询
    @RequestMapping("/daysCondition")
    public String days (Model model, @ModelAttribute("qo")DestinationDaysQuery qo){
        model.addAttribute("days",destinationService.queryDestDay(qo));

        //pageInfo 查询当前目的地下的游记
        //model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }



}
